Text in a program is a row of characters with a fixed maximum length that you choose when you write the code, and GBML gives you functions to join it, cut it, search it and turn numbers into it.
A program can hold a number, and it can hold words. A name typed on a title screen, a line of dialogue, the word SCORE above a number - all of these are text: a row of characters, in order. A character is one letter, digit, space or piece of punctuation.
GBML calls a piece of text a string, short for "a string of characters" - characters threaded one after another. Wherever this manual says string, it means exactly that: text the program is holding.
You write text between quote marks, either kind:
// Create event
var name = "Ada";
var line = 'Press START';The quotes tell GBML this is text and not a name. hp is a variable. "hp" is the two letters h and p. The same goes for digits: 10 is a number you can do arithmetic on, and "10" is the character 1 followed by the character 0. Adding two pieces of text joins them, so "10" + "5" is "105". Mixing the two kinds, as in "10" + 5, is an error - convert one side first (see Numbers to text, and back).
The text on the GBA is plain letters, digits and punctuation. The built-in font has no others, and the case-changing functions only touch the letters a to z.
A backslash \ inside the quotes starts a special character. These are the ones GBML knows:
| Write | You get |
|---|---|
\n | a new line: when the string is drawn, the text goes on from the next line |
\t | a tab |
\\ | one backslash |
\" | a double quote, without ending the string |
\' | a single quote, without ending the string |
// Draw GUI event
draw_text(4, 4, "SCORE 120\nBEST 450"); // two linesA backslash and any other letter is an error in the Problems list, and the message says which ones to use. A new line counts as one character, like any other, in string_length and in the room a string has. Drawing text says how the lines are laid out.
Put a $ in front of the quotes and anything in curly brackets is replaced by its value:
// Draw GUI event
var score = 120;
draw_text(4, 4, $"SCORE {score}"); // SCORE 120You can put a whole number, a fractional number, a piece of text or a yes-or-no value inside the brackets. A yes-or-no value is written into the text as true or false. This is called a template string.
Functions that point at a place in a piece of text count from 0, not 1. In "Ada", the character at position 0 is A, at 1 is d, and at 2 is a. The length is 3, so the last position is 2. string_length gives the length.
There is no separate type for a single character. A single character is just a piece of text one character long, which is why string_char_at hands one back as text.
== and != ask whether two pieces of text are identical, character for character. <, >, <= and >= put two pieces of text in order, the way a list of names is put in order. They look at the first character of each, then the second, and the first place where the two differ decides which one comes first. Each character is compared by its code (the number string_code_at gives), so a space comes before a digit, a digit before a capital letter, and every capital letter before every lowercase one: "Zed" comes before "ada", and "Apple" comes before "Banana". A character with a code above 127 comes after all the plain keyboard ones. A capital and a lowercase letter are different characters, so "apple" is not equal to "Apple".
A piece of text that is the beginning of a longer one comes first: "ab" is before "abc", and the empty text "" is before every other. The length never decides anything until one text runs out, so "b" is after "abc".
list_sort, array_sort and the other sorts of text put text in exactly this order, so a list that was sorted is in the order < says.
<, >, <= and >= work between two pieces of text, or between two numbers, and nothing else: name < 5 is refused, because a text has no place among numbers. Turn one into the other first, with string or string_to_number, whichever you meant.
// Step event
var name = "Ada";
var score = 0;
if (name == "Ada") { score += 10; }This is the rule that catches beginners, so it comes with the reason.
On a PC, a piece of text can simply grow: the computer finds more memory when it is needed. The Game Boy Advance cannot do that. Its memory is small and fixed, and the game gets no way to ask for more while it runs. So every piece of text is given its space once, when you write the code, and that space does not change afterwards.
The space is a number of characters. Every string variable has one, and it is the most characters that variable can ever hold. A variable with room for 32 characters can hold "Hello" and can hold a sentence of 32 letters. It can never hold 33.
Where the number comes from:
// Create event
/// @capacity 12
name = ""; // this one holds at most 12 charactersRoom costs memory whether or not the text fills it. A variable with room for 64 characters uses that memory even while it holds "Hi". So give a name-entry field the length a name needs, not the biggest number you can think of.
It depends on how the text got too long, so it is worth knowing each case:
| What you did | What happens |
|---|---|
Wrote a piece of text in your code that is longer than the variable's room, and the variable has its own @capacity tag | The compiler stops with an error and names the variable. The game is never built. |
| Wrote a piece of text that is longer than the project default, in a variable with no tag | The compiler does not check this one. The text does not fit, and a build with the safety checks on (the default) stops the game with an error screen when that line runs. |
Joined text with + or +=, or used string_join, string_slice, string_replace, string_repeat, string_insert, string_delete, string_upper, string_lower or string_trim | The text is cut off at the end. What fits is kept. The game carries on. string_repeat("ab", 1000) is one full string of abab..., not a thousand copies. |
Put the answer of one of those calls into a variable that has a smaller room (a @capacity tag) than the answer is long | The calls cut at the project's Longest string size, not at the size of the variable the answer goes into. If the answer is longer than that variable's room, the game stops with an error screen when the line runs (a build with the safety checks on, the default). Make it fit first, for example small = string_slice(answer, 0, 12);. |
Built a template string ($"...") whose finished text is longer than the room | The game stops with an error screen, the same as the row above. A template string is always built in a space of the project's Longest string size, even when it is headed for a variable with a bigger tag. |
Turned a number into text with string or inside a template string | The number is written in a space of 16 characters. That is enough for any number a game keeps. |
When a cut would matter, check it. string_length on the result is smaller than you expected if something was lost. For a template string, keep the total comfortably inside the limit, or raise Longest string in Project Settings.
Every function that changes text gives back a new piece of text. It never edits the one you gave it, so string_upper(name) leaves name as it was. Assign the result if you want to keep it: name = string_upper(name);.
| To | Use |
|---|---|
| Count the characters | string_length |
| Take one character | string_char_at - a position outside the text gives an empty piece of text, not an error |
| Take a part | string_slice - a start or length that runs off the end is trimmed to what exists |
| Find where something is | string_find - the position of the first match, or -1 when there is none |
| Replace every match | string_replace - the slowest of the group, fine for building a line of dialogue and not for every frame |
| Change case | string_upper, string_lower |
| Remove spaces from both ends | string_trim |
| Join two | string_join, or + |
| Write something several times in a row | string_repeat - an answer too long for the string is cut off at the end |
| Make columns line up | string_pad_left and string_pad_right add filler in front or behind. string_digits writes a whole number with zeros in front, for a clock or a score |
| Put text into the middle | string_insert - an answer too long for the string is cut off at the end |
| Take a part out | string_delete - the opposite of insert, never longer than what you gave it |
| Count how many times something appears | string_count |
| Get a character's number | string_code_at - the partner of string_char_at; a position outside the text gives -1 |
There is no function for "does this start with that". string_find(s, needle) == 0 answers it. Comparing needs no function at all, since == and < already work.
string_find reports -1 for "not found" because 0 is a real position. Test for it with != -1 rather than treating the answer as yes or no.
string writes a number out as text. A template string does the same job inline and is usually the shorter way to build a line. string_to_number goes the other way: it reads an optional minus sign, digits and an optional decimal point, and stops at the first character that does not belong. "12abc" reads as 12. Text with no number in it reads as 0.
Text held in a variable is invisible until you draw it. draw_text draws it at a position in the room when a Draw event calls it, and at a fixed place on the screen when a Draw GUI event does. See Numbers for how the numbers in {} behave.